package com.visionork.android.tai.activity;

import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.visionork.android.tai.TaiApp;
import com.visionork.android.tai.model.Program;
import com.visionork.android.tai.model.ProgramRepositoryBase;
import com.visionork.android.tai.model.ProgramType;

public class FavoriteProgramListActivity extends ProgramListActivityBase {

	public FavoriteProgramListActivity() {
		super();
	}

	@Override
	protected ProgramRepositoryBase getRepository() {
		return TaiApp.getFavoriteRepository();
	}

	@Override
	protected void onResume() {
		TaiApp.getFavoriteRepository().getPrograms(false);
		itemAdapter.notifyDataSetChanged();
		super.onResume();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.program_context_menu_remove, menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		switch(item.getItemId())
		{
		case R.id.toggle_favorite:
			Program prog = getRepository().getPrograms().get(info.position);
			prog.setFavorite(false);
			TaiApp.getFavoriteRepository().getPrograms(false);
			switch (prog.getProgramType()) {
			case ProgramType.Show:
				TaiApp.getShowRepository().saveToFile();
				break;				
			case ProgramType.Drama:
				TaiApp.getDramaRepository().saveToFile();
				break;
			}
			itemAdapter.notifyDataSetChanged();
			return true;
		case R.id.share:
			Program sharedProgram = getRepository().getPrograms().get(info.position);
			String subject = "FWD : " + sharedProgram.getTitle();
			String text = sharedProgram.getTitle() + " - " + sharedProgram.getUrlLink();
			TaiApp.share(this, subject, text);			
			return true;
		}
				
		return super.onMenuItemSelected(featureId, item);

	}
}
